package com.example.hobbyplanetserve.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hobbyplanetserve.config.DBUserDetailsManager;
import com.example.hobbyplanetserve.dto.DtoLogin;
import com.example.hobbyplanetserve.entity.MyTUserDetail;
import com.example.hobbyplanetserve.entity.User;
import com.example.hobbyplanetserve.mapper.UserMapper;
import com.example.hobbyplanetserve.properties.JwtProperties;
import com.example.hobbyplanetserve.service.UserService;
import com.example.hobbyplanetserve.utils.JwtUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
/**
* @author yuppie
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-02-11 18:09:49
*/
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Resource
    private DBUserDetailsManager userDetailsManager;

    @Autowired
    AuthenticationManager authenticationManager;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    JwtProperties jwtProperties;

    @Override
    public boolean saveUser(User user) {
        // 使用 PasswordEncoder 对密码进行编码
        String encodedPassword = passwordEncoder.encode(user.getPassword());

        // 创建 UserDetails 对象
        UserDetails userDetails = org.springframework.security.core.userdetails.User.builder()
                .username(user.getUsername())
                .password(encodedPassword) // 使用编码后的密
                .build();

        // 保存用户
        userDetailsManager.createUser(userDetails);

        return true;
    }

    @Override
    public String login(DtoLogin dtoLogin) {
        try {
            // 创建认证对象
            UsernamePasswordAuthenticationToken authenticationToken =
                    new UsernamePasswordAuthenticationToken(dtoLogin.getUsername(), dtoLogin.getPassword());

            // 调用 Spring Security 的认证逻辑
            Authentication authenticate = authenticationManager.authenticate(authenticationToken);

            // 获取返回的用户信息
            Object principal = authenticate.getPrincipal();

            MyTUserDetail userDetail = (MyTUserDetail) principal;

            Long id = userDetail.getUsers().getId();


            log.info("登陆的用户id为 {}",userDetail.getUsers().getId());

            //生产token
            String token = jwtUtils.generateToken(dtoLogin.getUsername(),id,jwtProperties.getAdminTtl(),jwtProperties.getAdminSecretKey());

            log.info("登录成功 生产的token为 {}",token);

            return token;

        } catch (AuthenticationException e) {
            log.error("登录失败: {}", e.getMessage());
            return null;
        }
    }
}




